package Review;
/*和单链表不一样，双链表引入了一个last引用来指向最后一个节点（一直是指向最后一个节点）
* 也就是说在开始没有节点时，head和last节点都是指向头节点的*/
/*所有的插入都是先绑后边*/
public class MyLinkedList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;
    //打印遍历链表
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //头插法O(1)
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    //尾插法O(1):因为省去了找尾巴的过程
    public void addLast(int data) {
        //如果链表中没有元素
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }

    }

    //任意位置插入，第一个数据节点为0号的下标
    public void addIndex(int index, int data) {
        if (index < 0 || index >size()) {
            throw  new ListIndexOutOfException();
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        //不是头插不是尾插，在中间插入一个元素
        ListNode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        ListNode node = new ListNode(data);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
    //查找是否包含关键字key在链表中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    // 删除关键字为key的节点
    public void remove(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                //1.删除的是头节点
                if (cur == head) {
                    head = head.next;
                    //如果head.next是空的(只有一个节点)，此时空指针异常
                    if (head != null) {
                        head.prev = null;
                    }
                } else {
                    //中间  尾巴
                    cur.prev.next = cur.next;
                    //不是尾巴节点
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        //是尾巴节点
                        last = last.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }

    //删除所有值为key的节点
    public void removeKeyAll(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                //1.删除的是头节点
                if (cur == head) {
                    head = head.next;
                    //如果head.next是空的(只有一个节点)，此时空指针异常
                    if (head != null) {
                        head.prev = null;
                    }
                } else {
                    //中间  尾巴
                    cur.prev.next = cur.next;
                    //不是尾巴节点
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        //是尾巴节点
                        last = last.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }
    //求长度
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
}
